<cfcomponent output="false">
	
	<!--- --->

	<cffunction name="getGoals" access="public" output="true" returntype="query">

		<cfreturn application.g.goalGateway.getGoals(argumentCollection=arguments) />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="getMilestones" access="public" output="true" returntype="query">

		<cfreturn application.g.goalGateway.getMilestones(argumentCollection=arguments) />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="getActions" access="public" output="true" returntype="query">

		<cfreturn application.g.goalGateway.getActions(argumentCollection=arguments) />
		
	</cffunction>
	
	<!--- --->

	<cffunction name="deleteGoal" access="public" output="false" returntype="string">
		<cfargument name="goalID" required="true" />
		
		<cfset var local = {} />
		
		<cfset $.database.delete(
				table="goal",
				id=arguments.goalID
			) />
		
		<cfreturn true />
		
	</cffunction>

	<!--- --->
	
	<cffunction name="deleteMilestone" access="public" output="false" returntype="string">
		<cfargument name="milestoneID" required="true" />
		
		<cfset var local = {} />
		
		<cfset $.database.delete(
				table="goal_milestone",
				id=arguments.milestoneID
			) />
		
		<cfreturn true />
		
	</cffunction>

	<!--- --->
	
	<cffunction name="deleteAction" access="public" output="false" returntype="string">
		<cfargument name="actionID" required="true" />
		
		<cfset var local = {} />
		
		<cfset $.database.delete(
			table="goal_action",
			id=arguments.actionID
		) />

		<cfreturn true />
		
	</cffunction>

	<!--- --->

	<cffunction name="saveGoal" access="public" output="false" returntype="string">
		<cfargument name="goalID" required="false" default="" />
		<cfargument name="userID" required="true" default="#request.session.user.id#" />
		<cfargument name="name" required="false" default="" />
		<cfargument name="description" required="false" default="" />
		<cfargument name="start" required="false" default="" />
		<cfargument name="stop" required="false" default="" />
		<cfargument name="abandoned" required="false" default="0" />
		<cfargument name="measurable" required="false" default="0" />
		<cfargument name="unit" required="false" default="" />
		
		<cfset var local = {} />
		
		<cfset local.start = $.date.localToUTC(arguments.start & " 00:00:00") />
		<cfset local.stop = $.date.localToUTC(arguments.stop & " 00:00:00") />

		<cfif arguments.abandoned eq true>
			<cfset arguments.abandoned = 1 />
		<cfelseif arguments.abandoned eq false>
			<cfset arguments.abandoned = 0 />
		</cfif>

		<cfif arguments.measurable eq true>
			<cfset arguments.measurable = 1 />
		<cfelseif arguments.measurable eq false>
			<cfset arguments.measurable = 0 />
		</cfif>

		<cfset local.goalID = $.database.save(
			table="goal",
			id=arguments.goalID,
			user_id=arguments.userID,
			name=arguments.name,
			description=arguments.description,
			start=local.start,
			stop=local.stop,
			abandoned=arguments.abandoned,
			measurable=arguments.measurable,
			unit=arguments.unit
		) />
		
		<cfreturn local.goalID />
		
	</cffunction>

	<!--- --->

	<cffunction name="saveMilestone" access="public" output="false" returntype="string">
		<cfargument name="milestoneID" required="false" default="" />
		<cfargument name="goalID" required="true" />
		<cfargument name="date" required="true" />
		<cfargument name="description" required="true" />
		<cfargument name="complete" required="false" default="0" />
		<cfargument name="locked" required="false" default="0" />
		<cfargument name="target_count" required="false" default="" />
		<cfargument name="actual_count" required="false" default="" />
		
		<cfset var local = {} />

		<cfset local.date = $.date.localToUTC(arguments.date) />
		
		<cfset local.complete = 0 />
		<cfif arguments.complete>
			<cfset local.complete = 1 />
		</cfif>

		<cfset local.args = structNew() />
		<cfset local.args.table = "goal_milestone" />
		<cfset local.args.id = arguments.milestoneID />
		<cfset local.args.goal_id = arguments.goalID />
		<cfset local.args.description = arguments.description />
		<cfset local.args.date = local.date />
		<cfset local.args.complete = local.complete />
		<cfset local.args.locked = arguments.locked />
		<cfset local.args.actual_count = arguments.actual_count />
		<cfset local.args.target_count = arguments.target_count />
		
		<cfif arguments.target_count neq "" and arguments.actual_count neq "" and isNumeric(arguments.target_count) and isNumeric(arguments.actual_count)>
			<cfif arguments.actual_count gte arguments.target_count>
				<cfset local.args.complete = 1 />
				<!--- also set all previous milestones to complete --->
				<cfset local.milestones = $.run.it('goalService.getMilestones',{
					goalID=arguments.goalID,
					complete=0,
					enddate=arguments.date
				}) />
				<cfloop query="local.milestones">
					<cfset $.database.save(
						table="goal_milestone",
						id=local.milestones.id,
						complete=1							
					) />
				</cfloop>
			</cfif>
		</cfif>

		<cftransaction>
			<cfset local.id = $.database.save(argumentCollection=local.args) />
			
			<!--- update the goal's dates if not a new milestone --->
			<cfif arguments.locked neq 0 and arguments.milestoneID neq "" and arguments.goalID neq "">
				<cfif arguments.locked eq -1>
					<cfset $.database.save(
						table="goal",
						id=arguments.goalID,
						start=local.date
					) />
				</cfif>
				<cfif arguments.locked eq 1>
					<cfset $.database.save(
						table="goal",
						id=arguments.goalID,
						stop=local.date		
					) />
				</cfif>
			</cfif>
		</cftransaction>
		
		<cfreturn local.id />
		
	</cffunction>

	<!--- --->
	
	<cffunction name="saveMilestoneComplete" access="public" output="false" returntype="string">
		<cfargument name="milestoneID" required="true" />
		<cfargument name="complete" required="true" />
		
		<cfset var local = {} />
		
		<cfset local.complete = 0 />
		<cfif arguments.complete>
			<cfset local.complete = 1 />
		</cfif>
	
		<cfset local.id = $.database.save(
			table="goal_milestone",
			id=arguments.milestoneID,
			complete=local.complete
		) />
		
		<cfreturn local.complete />
		
	</cffunction>

	<!--- --->

	<cffunction name="saveAction" access="public" output="false" returntype="string">
		<cfargument name="actionID" required="false" default="" />
		<cfargument name="goalID" required="true" />
		<cfargument name="name" required="true" />
		<cfargument name="milestoneID" required="false" default="" />
		
		<cfset var local = {} />

		<cfset local.id = $.database.save(
			table="goal_action",
			id=arguments.actionID,
			goal_id=arguments.goalID,
			name=arguments.name,
			goal_milestone_id=arguments.milestoneID
		) />
		
		<cfreturn local.id />
		
	</cffunction>

	<!--- --->

	<cffunction name="bulkDeleteMilestones" access="public" output="true" returntype="void">

		<cfreturn application.g.goalGateway.bulkDeleteMilestones(argumentCollection=arguments) />
		
	</cffunction>
	
	<!--- --->

	<cffunction name="bulkDeleteActions" access="public" output="true" returntype="void">

		<cfreturn application.g.goalGateway.bulkDeleteActions(argumentCollection=arguments) />
		
	</cffunction>
	
	<!--- --->

	<cffunction name="getUpcomingMilestones" access="public" output="true" returntype="query">
		<cfargument name="startdate" required="false" default="#$.date.getDate()#" />
		<cfargument name="enddate" required="false" default="#dateAdd('d', 45, $.date.getDate())#" />
		<cfargument name="userID" required="false" default="#request.session.user.id#" />
		
		<cfreturn application.g.goalGateway.getMilestones(argumentCollection=arguments) />

	</cffunction>

	<!--- --->
	
	<cffunction name="generateMilestones" access="public" output="true" returntype="string">
		<cfargument name="goalID" required="true" />
		<cfargument name="startdate" required="true" />
		<cfargument name="enddate" required="true" />
		<cfargument name="userID" required="false" default="#request.session.user.id#" />
		<cfargument name="milestone_frequency" required="true" />
		
		<cfset var local = {} />
		
		<cfset arguments.startdate = arguments.startdate & " 00:00:00" />
		<cfset arguments.enddate = arguments.enddate & " 00:00:00" />
		
		<cfif arguments.milestone_frequency eq "monthly">
			<cfset local.datepart = "m" />
			<cfset local.quantity = 1 />
			<cfset local.max = 12 />
		<cfelseif arguments.milestone_frequency eq "weekly">
			<cfset local.datepart = "ww" />
			<cfset local.quantity = 1 />
			<cfset local.max = 52 />
		<cfelseif arguments.milestone_frequency eq "biweekly">
			<cfset local.datepart = "ww" />
			<cfset local.quantity = 2 />
			<cfset local.max = 26 />
		<cfelseif arguments.milestone_frequency eq "bimonthly">
			<cfset local.datepart = "x" />
			<cfset local.quantity = 1 />
			<cfset local.max = 24 />
		<cfelseif arguments.milestone_frequency eq "quarterly">
			<cfset local.datepart = "q" />
			<cfset local.quantity = 1 />
			<cfset local.max = 4 />
		<cfelse>
			<cfreturn false />
		</cfif>
		
		<!--- generate milestones based on frequency --->
		<cfset local.milestoneDate = arguments.startDate />
		<cfloop from="1" to="#local.max#" index="local.i">
			<cfif arguments.milestone_frequency eq "bimonthly">
				<cfif day(local.milestoneDate) lt 13>
					<cfset local.milestoneDate = dateAdd('d',15-day(local.milestoneDate),local.milestoneDate) />
				<cfelse>
					<cfset local.daysInMonth = daysInMonth(local.milestoneDate) />
					<cfset local.milestoneDate = dateAdd('d',local.daysInMonth - day(local.milestoneDate)+1,local.milestoneDate)>
				</cfif>
			<cfelse>
				<cfset local.milestoneDate = dateAdd(local.datepart,local.quantity,local.milestoneDate) />
			</cfif>
			<cfif dateCompare(local.milestoneDate,arguments.endDate) eq -1>
				<cfset $.run.it('goalService.saveMilestone',{
					goalID=arguments.goalID,
					date=dateFormat(local.milestoneDate,'mm/dd/yyyy'),
					description="Milestone #local.i#"
				}) />
			<cfelse>
				<cfbreak />
			</cfif>
		</cfloop>

		<!--- get them milestones, and generate target numbers --->
		<cfif arguments.target neq "" and isNumeric(arguments.target) and arguments.target gte 1>
			<cfset local.milestones = $.run.it('goalService.getMilestones',{goalID=arguments.goalID,order="goal_milestone.date desc"}) />
			<!--- avoid divide by zero --->
			<cfif local.milestones.recordcount lt 2>
				<cfreturn true />
			</cfif>
			
			<cfset local.increment = arguments.target / (local.milestones.recordcount - 1) />
			<!--- in case the result is less than 1 --->
			<cfif local.increment lt 1>
				<cfset local.increment = 1 />
			</cfif>
			<cfloop query="local.milestones">
				<!--- don't let start ever be > 0 --->
				<cfif local.milestones.recordcount eq local.milestones.currentRow>
					<cfset arguments.target = 0 />
				</cfif>
				<cfset $.database.save(
					table="goal_milestone",
					id=local.milestones.id,
					target_count=fix(arguments.target)
				) />
				<!--- decrement target --->
				<cfset arguments.target = arguments.target - local.increment />
				<cfif arguments.target lt 0>
					<cfset arguments.target = 0 />
				</cfif>
			</cfloop>
		</cfif>
		
		<cfreturn true />

	</cffunction>

	<!--- --->

</cfcomponent>